﻿Public Class frmEmpleadoCE 

    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If



    End Sub
 

    Private Sub frmEmpleadoCE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empleados' table. You can move, or remove it, as needed.
        Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empleadoestado' table. You can move, or remove it, as needed.
        Me.EmpleadoestadoTableAdapter.Fill(Me.DbFireAndIceDataSet.empleadoestado)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empleados' table. You can move, or remove it, as needed.
        Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empleadoestado' table. You can move, or remove it, as needed.
        Me.EmpleadoestadoTableAdapter.Fill(Me.DbFireAndIceDataSet.empleadoestado)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empresaestado' table. You can move, or remove it, as needed.

      


    End Sub


   

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
       
        FechaDateTimePicker.Value = FechaDateTimePicker.Value.Date.ToString("dd/MM/yyyy")





        If IdempleadoLookUpEdit.Text = "" Then
            ErrorProvider1.SetError(IdempleadoLookUpEdit, "Seleccione un empleado")
            Return

        End If
        ErrorProvider1.SetError(IdempleadoLookUpEdit, Nothing)

        If EstadoCheckBox.CheckState = CheckState.Indeterminate Then
            ErrorProvider1.SetError(EstadoCheckBox, "Debe seleccionar un estado(checked=activo / unchecked=inactivo")
            Return
        End If
        ErrorProvider1.SetError(EstadoCheckBox, Nothing)

        Try
            ' EmpleadosTableAdapter.UpdateQueryActualizaestado(EstadoCheckBox.CheckState, IdempleadoLookUpEdit.GetColumnValue("idempleado"))

            EmpleadoestadoBindingNavigatorSaveItem.PerformClick()

            Me.EmpleadoestadoTableAdapter.Fill(Me.DbFireAndIceDataSet.empleadoestado)

            Mostrar()
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "Nose pudo guardar el registro")
            Mostrar()
        End Try
        AlertControl1.AppearanceCaption.ForeColor = Color.Green
        AlertControl1.Show(Me, "Advertencia", My.Resources.StringGlobal.msjGuardarExito)


        'EmpleadosTableAdapter.Update(IdempleadoLookUpEdit.Text)

    End Sub

    Private Sub EmpleadoestadoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.EmpleadoestadoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)


    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        mostrar()

    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(IdempleadoLookUpEdit, Nothing)
        ErrorProvider1.SetError(EstadoCheckBox, Nothing)
        Me.EmpleadoestadoTableAdapter.Fill(Me.DbFireAndIceDataSet.empleadoestado)
        Mostrar()
    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()

    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                EmpleadoestadoTableAdapter.DeleteQuery(IdempleadoestadoTextBox.Text)
                Me.EmpleadoestadoTableAdapter.Fill(Me.DbFireAndIceDataSet.empleadoestado)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub

    Private Sub EmpleadoestadoBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.EmpleadoestadoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub EmpleadoestadoBindingNavigatorSaveItem_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmpleadoestadoBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.EmpleadoestadoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub IdempleadoLookUpEdit_EditValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdempleadoLookUpEdit.EditValueChanged
        txtnombre.EditValue = IdempleadoLookUpEdit.GetColumnValue("nombre")
        txtapellido.EditValue = IdempleadoLookUpEdit.GetColumnValue("apellido")

    End Sub
End Class